package lab6.systemCustom;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class DataBase {

	protected ArrayList vStudent;

	protected ArrayList vCourse;

	public DataBase(String sStudentFileName, String sCourseFileName) throws FileNotFoundException, IOException {

		BufferedReader objStudentFile = new BufferedReader(new FileReader(sStudentFileName));
		BufferedReader objCourseFile = new BufferedReader(new FileReader(sCourseFileName));

		this.vStudent = new ArrayList();
		this.vCourse = new ArrayList();

		while (objStudentFile.ready()) {
			this.vStudent.add(new Student(objStudentFile.readLine()));
		}
		while (objCourseFile.ready()) {
			this.vCourse.add(new Course(objCourseFile.readLine()));
		}

		objStudentFile.close();
		objCourseFile.close();
	}

	public ArrayList getAllStudentRecords() {
		return this.vStudent;
	}

	public ArrayList getAllCourseRecords() {
		return this.vCourse;
	}

	public Student getStudentRecord(String sSID) {

		for (int i = 0; i < this.vStudent.size(); i++) {
			Student objStudent = (Student) this.vStudent.get(i);
			if (objStudent.match(sSID)) {
				return objStudent;
			}
		}

		return null;
	}

	public String getStudentName(String sSID) {
		for (int i = 0; i < this.vStudent.size(); i++) {
			Student objStudent = (Student) this.vStudent.get(i);
			if (objStudent.match(sSID)) {
				return objStudent.getName();
			}
		}

		return null;
	}

	public Course getCourseRecord(String sCID) {
		for (int i = 0; i < this.vCourse.size(); i++) {
			Course objCourse = (Course) this.vCourse.get(i);
			if (objCourse.match(sCID)) {
				return objCourse;
			}
		}

		return null;
	}

	public String getCourseName(String sCID) {
		for (int i = 0; i < this.vCourse.size(); i++) {
			Course objCourse = (Course) this.vCourse.get(i);
			if (objCourse.match(sCID)) {
				return objCourse.getName();
			}
		}

		return null;
	}

	public void makeARegistration(String sSID, String sCID) {
		Student objStudent = this.getStudentRecord(sSID);
		Course objCourse = this.getCourseRecord(sCID);

		if (objStudent != null && objCourse != null) {
			objStudent.registerCourse(objCourse);
			objCourse.registerStudent(objStudent);
		}
	}

}